package com.ztgf.commodity.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommoditySpecificationMapper;
import com.ztgf.commodity.models.bo.AttributesQueryBO;
import com.ztgf.commodity.models.bo.CommoditySpecificationNewSaveBO;
import com.ztgf.commodity.models.bo.CommoditySpecificationSaveBO;
import com.ztgf.commodity.models.dto.CommoditySpecificationAttrDTO;
import com.ztgf.commodity.models.dto.CommoditySpecificationDTO;
import com.ztgf.commodity.models.service.ICommoditySpecificationAttrService;
import com.ztgf.commodity.models.service.ICommoditySpecificationService;
import com.ztgf.commodity.models.vo.CommoditySpecificationVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.*;

/**
 * author: chenyu.wang
 * date: 2020-05-26 14:25
 * Description:
 */
@Slf4j
@DubboService
public class CommoditySpecificationServiceImpl extends ServiceImpl<CommoditySpecificationMapper, CommoditySpecificationDTO>
        implements ICommoditySpecificationService {

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private ICommoditySpecificationAttrService commoditySpecificationAttrService;


    @Override
    public boolean add(CommoditySpecificationSaveBO bo) {
        CommoditySpecificationDTO dto = new CommoditySpecificationDTO();
        //查询判断是否有相同名称
        Wrapper<CommoditySpecificationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("name", bo.getName());
        List<CommoditySpecificationDTO> dtos = this.selectList(wrapper);
        if (dtos != null && dtos.size() > 0) {
            return false;
        }
        BeanUtils.copyProperties(bo, dto);
        dto.setId(ymlConfig.getPkId());
        return this.insert(dto);
    }

    @Override
    public boolean deleteLogic(Long id) {
        CommoditySpecificationDTO dto = this.selectById(id);
        if (dto != null) {
            CommoditySpecificationDTO updateDto = new CommoditySpecificationDTO();
            updateDto.setId(dto.getId());
            if (dto.getStatus().equals(0)) {
                updateDto.setStatus(1);
            } else {
                updateDto.setStatus(0);
            }
            return this.updateById(updateDto);
        }
        return false;
    }

    @Override
    public CommoditySpecificationVO getById(Long id) {
        return baseMapper.getById(id);
    }

    @Override
    public Page<CommoditySpecificationVO> queryPageList(AttributesQueryBO bo) {
        Page<CommoditySpecificationVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<CommoditySpecificationVO> list = baseMapper.queryPageList(page, bo);
        page.setRecords(list);
        return page;
    }

    /*
     * @description:新的添加
     * @author: chenyu.wang
     * @date: 2020-09-03
     */
    @Transactional
    @Override
    public Result<List<String>> newAdd(String object) {
        Result<List<String>> result = new Result<>();
        Long id = ymlConfig.getPkId();
        CommoditySpecificationNewSaveBO bo = JSONObject.parseObject(object, CommoditySpecificationNewSaveBO.class);
        List<CommoditySpecificationAttrDTO> attrBOS = JSONObject.parseArray(bo.getAttrList(), CommoditySpecificationAttrDTO.class);
        //保存规格
        CommoditySpecificationDTO dto = new CommoditySpecificationDTO();
        dto.setName(bo.getName());//名称
        dto.setSort(bo.getSort());//排序
        dto.setStatus(ZERO);//状态
        dto.setIsPublish(1);//是否发布
        dto.setId(id);//id
        dto.setNotes(bo.getNotes());//备注
        boolean insert = this.insert(dto);
        if (!insert) {
            result.setCode(CodeEnum.FAIL_100007.getCode());
            result.setMsg(CodeEnum.FAIL_100007.getMsg());
            return result;
        }
        if (isNull(attrBOS)) {
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg(CodeEnum.SUCCESS.getMsg());
            return result;
        }
        //保存属性
        List<CommoditySpecificationAttrDTO> attrDTOList = new ArrayList<>();
        for (CommoditySpecificationAttrDTO attrBO : attrBOS) {
            CommoditySpecificationAttrDTO attrDTO = new CommoditySpecificationAttrDTO();
            attrDTO.setSpecificationId(id);
            attrDTO.setAttrName(attrBO.getAttrName());
            attrDTO.setStatus(ZERO);
            attrDTO.setIsPublish(ONE);
            attrDTO.setId(ymlConfig.getPkId());
            attrDTOList.add(attrDTO);
        }
        boolean insertBatch = commoditySpecificationAttrService.insertBatch(attrDTOList);
        if (!insertBatch) {
            result.setCode(CodeEnum.FAIL_100007.getCode());
            result.setMsg(CodeEnum.FAIL_100007.getMsg());
            return result;
        }
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setMsg(CodeEnum.SUCCESS.getMsg());
        return result;
    }

    /*
     * @description:新的规格属性修改
     * @author: chenyu.wang
     * @date: 2020-09-03
     */
    @Transactional
    @Override
    public Result<List<String>> newUpdate(String object) {
        Result<List<String>> result = new Result<>();
        CommoditySpecificationNewSaveBO bo = JSONObject.parseObject(object, CommoditySpecificationNewSaveBO.class);
        List<CommoditySpecificationAttrDTO> attrBOS = JSONObject.parseArray(bo.getAttrList(), CommoditySpecificationAttrDTO.class);
        //保存规格
        CommoditySpecificationDTO dto = new CommoditySpecificationDTO();
        dto.setName(bo.getName());
        dto.setSort(bo.getSort());
        dto.setStatus(ZERO);
        dto.setIsPublish(1);
        dto.setId(bo.getId());
        dto.setNotes(bo.getNotes());
        boolean update = this.updateById(dto);
        if (!update) {
            result.setCode(CodeEnum.FAIL_100008.getCode());
            result.setMsg(CodeEnum.FAIL_100008.getMsg());
            return result;
        }
        if (isNull(attrBOS)) {
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg(CodeEnum.SUCCESS.getMsg());
            return result;
        }
        //保存属性
        CommoditySpecificationAttrDTO attrDTO;
        for (CommoditySpecificationAttrDTO attrBO : attrBOS) {
            attrDTO = new CommoditySpecificationAttrDTO();
            if (null == attrBO.getId()) {
                //新增
                attrDTO.setId(ymlConfig.getPkId());
                attrDTO.setSpecificationId(bo.getId());
                attrDTO.setAttrName(attrBO.getAttrName());
                attrDTO.setStatus(ZERO);
                attrDTO.setIsPublish(1);
                commoditySpecificationAttrService.insert(attrDTO);
            } else {
                attrDTO.setId(attrBO.getId());
                attrDTO.setAttrName(attrBO.getAttrName());
                attrDTO.setStatus(ZERO);
                commoditySpecificationAttrService.updateById(attrDTO);
            }
        }
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setMsg(CodeEnum.SUCCESS.getMsg());
        return result;
    }

    @Override
    public Page<CommoditySpecificationVO> newQueryPageList(AttributesQueryBO bo) {
        Page<CommoditySpecificationVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<CommoditySpecificationVO> list = baseMapper.queryPageList(page, bo);
        if (!isNull(list)) {
            page.setRecords(list);
            return page;
        }
        Page<CommoditySpecificationAttrDTO> attrPage = new Page<>(bo.getPageNo(), bo.getPageSize());
        Wrapper<CommoditySpecificationAttrDTO> wrapper = new EntityWrapper<>();
        wrapper.like("attr_name", bo.getAttributesName());
        List<CommoditySpecificationAttrDTO> attrDTOList = commoditySpecificationAttrService.selectPage(attrPage, wrapper).getRecords();
        if (isNull(attrDTOList)) {
            return null;
        }
        List<String> ids = new ArrayList<>();
        for (CommoditySpecificationAttrDTO attrDTO : attrDTOList) {
            if (ids.contains(attrDTO.getSpecificationId().toString())) {
                continue;
            } else {
                ids.add(attrDTO.getSpecificationId().toString());
            }
        }
        if (isNull(ids)) {
            return null;
        }
        List<CommoditySpecificationVO> realList = new ArrayList<>();
        for (String id : ids) {
            CommoditySpecificationDTO dto = this.selectById(id);
            if (null != dto) {
                CommoditySpecificationVO specificationVO = new CommoditySpecificationVO();
                List<CommoditySpecificationAttrDTO> attrDTOS = baseMapper.getBySpecId(dto.getId());
                specificationVO.setId(dto.getId());
                specificationVO.setName(dto.getName());
                specificationVO.setState(dto.getStatus());
                specificationVO.setIsPublish(dto.getIsPublish());
                specificationVO.setSort(dto.getSort());
                specificationVO.setNotes(dto.getNotes());
                if (!isNull(attrDTOS)) {
                    specificationVO.setAttrList(attrDTOS);
                }
                realList.add(specificationVO);
            }
        }
        page.setRecords(realList);
        return page;
    }
}
